/**
 * 简易验证中国大陆手机号
 *
 * @param phone 手机号
 */
export const mobileValidator = (phone: string): boolean => {
  const reg = /^1\d{10}$/

  return reg.test(phone)
}

/**
 * 简易验证电子邮箱地址
 *
 * @param email 电子邮箱地址
 */
export const emailValidator = (email: string): boolean => {
  const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/

  return reg.test(email)
}

/**
 * 简易验证中国居民身份证（第三代身份证）
 *
 * @param idCard 身份证号
 */
export const idCardValidator = (idCard: string): boolean => {
  if (!/^\d{17}[0-9xX]$/.test(idCard)) {
    return false
  }

  let verify = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  let code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
  let sum = 0
  for (let i = 0; i < 17; i++) {
    sum += Number(idCard[i]) * verify[i]
  }
  let last = code[sum % 11]

  return idCard[17].toUpperCase() === last
}

/**
 * 简易验证 URL
 *
 * @param url URL 地址
 */
export const URLValidator = (url: string): boolean => {
  const reg = /^((https?|ftp):)?\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|cn|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/

  return reg.test(url)
}

/**
 * 简易验证网络图片
 *
 * @param url 网络图片 URL
 */
export const imageValidator = (url: string): boolean => {
  const reg = /^(https?:)?\/\/.*\.(jpe?g|png|svg|gif|bmp|webp)([#?]+.*)?$/

  return reg.test(url.toLowerCase())
}
